/**
 * all unit movement should be handled through one of these
 */

package ai;

import gui.GamePanel;

import java.awt.Color;
import java.util.*;

import mapData.Coord;
import mapObjects.Unit;

public class UnitGroup 
{
	private ArrayList<Unit> group = new ArrayList<Unit>();
	private Color groupColor;
	
	private ArrayList<Unit> unitQueue; //ptr to pathfinding queue
	
	
	public UnitGroup(ArrayList<Unit> preFormed, Color teamCol, ArrayList<Unit> q)
	{
		group = preFormed;
		groupColor = teamCol;
		unitQueue = q;
		
	}
	
	public UnitGroup(){
		
	}
	
	
	
	public UnitGroup(Color teamCol, ArrayList<Unit> q)
	{
		
		groupColor = teamCol;
		unitQueue = q;
		
	}
	
	public void clearGroup()
	{
	    group.clear();
	}
	
	public int groupSize()
	{
	    return group.size();
	}
	
	public Unit groupMember(int i){
		return group.get(i);
	}
	
	/**
	 * Returns true if unit was successfully added,
	 * else returns false.
	 * @param addThis
	 * 
	 */	
	public boolean addUnit(Unit addThis)
	{
	    
	    if(addThis.getColor()!= groupColor)
	    {
	    	return false;
	    }
	    for(int i = 0; i < group.size(); i++)
	    {
			if(group.get(i) == addThis) //match address and team Color
			{
			    return false;
			}
	    }
	    group.add(addThis);
	    addThis.setCurrentGoup(this);
	    return true;
	}
	
	
	public void setGroupDest(Coord goal)
	{
	    //groupDest = goal;
		ArrayList<Unit> temp = new ArrayList<Unit>();
	    for(int i = 0; i < group.size(); i++)
	    {
	    	group.get(i).setDest(goal);
	    	temp.add(group.get(i));
	    	//destQueue.add(goal);
	    }
	    GamePanel.pfinder.sortUnitByMDistance(temp, goal);
	    unitQueue.addAll(temp);
	}

	public void setGroupTarget(Unit occupier) 
	{
		for(int i = 0; i < group.size(); i++)
	    {
			
	    	group.get(i).setTarget(occupier);
	    	
	    }
				
	}
	
	public void removeUnit(Unit toBeRemoved)
	{
		group.remove(toBeRemoved);
		unitQueue.remove(toBeRemoved);
		
	}
}
